/**
  * 投诉客服
  */
module game {

    export class TouSuKeFuMediator extends BaseMediator {
        public static NAME: string = "TouSuKeFuMediator";
        public constructor(viewComponent: any = null) {
            super(TouSuKeFuMediator.NAME, viewComponent);
        }

        public listNotificationInterests(): Array<any> {
            return [
                PanelNotify.HALL.STAGE_RESIZE,
                PanelNotify.HALL.OPEN_TOUSU_KEFU,
                PanelNotify.HALL.CLOSE_TOUSU_KEFU
            ];
        }
        private panel: TouShuKeFuPanel;
        private QQNumber = null;
        public handleNotification(notification: puremvc.INotification): void {

            switch (notification.getName()) {
                case PanelNotify.HALL.STAGE_RESIZE:
                    if (this.panel) {
                        this.panel.width = GameConfig.curWidth()
                        this.panel.height = GameConfig.curHeight()
                    }
                    break;
                case PanelNotify.HALL.OPEN_TOUSU_KEFU: {
                    this.QQNumber = notification.getBody();
                    this.panel = null;
                    this.panel = new TouShuKeFuPanel();
                    this.showUI(this.panel, 2, 1, true);
                    break;
                }
                case PanelNotify.HALL.CLOSE_TOUSU_KEFU: {
                    this.closePanel(0);
                    break;
                }
            }
        }

        /**
         * 初始化面板ui
         */
        public initUI(): void {
            this.panel.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closeButtonClick, this);
            this.panel.confirm.addEventListener(egret.TouchEvent.TOUCH_TAP, this.confirmBrokeButtonClick, this);
        }

        /**
         * 初始化面板数据
         */
        public initData(): void {
            if (this.QQNumber) {
                this.panel.QQNumber.text = this.QQNumber + "";
                this.panel.QQNumber.touchEnabled = false
            } else {
                this.panel.QQNumber.text = "";
                this.panel.QQNumber.touchEnabled = true
            }
        }
        private closeButtonClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            this.closePanel(1);
        }
        private confirmBrokeButtonClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            var text: string = this.panel.text.text;
            var QQ = this.panel.QQNumber.text
            if (QQ == "" || QQ == null) {
                EffectUtils.textFadeAway("请输入客服QQ号")//弹出提示消息
                return;
            }
            var text = text.replace(/^\s+|\s+$/g, "");
            if (text.length < 15 || text.length > 150) {
                EffectUtils.textFadeAway("提交内容必须为15-150个字符")//弹出提示消息
                return;
            }
            var time = Math.floor(Global.getCurrTimeLong() / 1000)
            var signature = Global.getSynLoginKey(time)
            signature = signature.toUpperCase()
            var request = new egret.HttpRequest();
            request.responseType = egret.HttpResponseType.TEXT;
            request.open(InstructionsEnum.IP_API + "/GameWebApplication/SubmitMessage.ashx?userid=" + GameVO.getVo().dwUserID + "&signature=" + signature + "&time=" + time, egret.HttpMethod.POST);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                EffectUtils.hideWaritPanel()
                try {
                    var request = <egret.HttpRequest>event.currentTarget;
                    var obj = JSON.parse(request.response)
                    if (obj.code == 0) {
                        EffectUtils.textFadeAway("投诉成功!")
                    } else {
                        Global.tipShow(obj.msg)
                    }
                } catch (err) {
                    EffectUtils.textFadeAway("数据包异常，无法解析!")
                }
            }, this);
            request.addEventListener(egret.IOErrorEvent.IO_ERROR, () => {
                EffectUtils.hideWaritPanel()
                Global.tipShow("网络请求失败，请稍后重试!")
            }, this);
            EffectUtils.showWaritPanel()
            var obj = { MsgContent: text, MsgTiltle: QQ, UserId: GameVO.getVo().dwUserID, MsgType: 1 };
            request.send(JSON.stringify(obj));
            this.closePanel(1)
        }
    }
}
